package io.renren.modules.projectinfo.controller;

import java.util.*;

import io.renren.common.annotation.SysLog;
import io.renren.common.utils.ConstantUtils;
import io.renren.common.validator.ValidatorUtils;
import io.renren.common.validator.group.AddGroup;
import io.renren.modules.areainfos.service.AreainfosService;
import io.renren.modules.collect.service.CollectinfosService;
import io.renren.modules.footprint.service.VipfootprintinfosService;
import io.renren.modules.projectinfo.entity.ProjectinfosCustomEntity;
import io.renren.modules.sys.controller.AbstractController;
import io.renren.modules.user.service.UserinfosService;
import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import io.renren.modules.projectinfo.entity.ProjectinfosEntity;
import io.renren.modules.projectinfo.service.ProjectinfosService;
import io.renren.common.utils.PageUtils;
import io.renren.common.utils.Query;
import io.renren.common.utils.R;



/**
 * 
 * 
 * @author 黄清
 * @email
 * @date 2018-10-18 15:20:31
 */
@RestController
@RequestMapping("/projectinfo/projectinfos")
public class ProjectinfosController extends AbstractController {
	@Autowired
	private ProjectinfosService projectinfosService;

	@Autowired
	private UserinfosService userinfosService;

	@Autowired
	private AreainfosService areainfosService;

	@Autowired
	private CollectinfosService collectinfosService;

	@Autowired
	private VipfootprintinfosService vipfootprintinfosService;

	//查询审核看到的信息
	@SysLog("查询审核项目列表")
	@RequestMapping("/auditlist")
	@RequiresPermissions("projectinfo:projectinfos:auditlist")
	public R queryAuditProject(@RequestParam Map<String, Object> params){
		if(params == null){
			return R.error("不能为空哦！");
		}
		//查询列表数据
		Query query = new Query(params);
		List<ProjectinfosCustomEntity> projectinfosList = projectinfosService.queryAuditList(query);
		int total = projectinfosService.queryAuditTotal(query);
		PageUtils pageUtil = new PageUtils(projectinfosList, total, query.getLimit(), query.getPage());
		return R.ok().put("page", pageUtil);
	}

	//审核项目
	@SysLog("审核项目")
	@PostMapping("/audit")
	@RequiresPermissions("projectinfo:projectinfos:audit")
	public R auditProject(@RequestBody ProjectinfosEntity projectinfos){
		if(projectinfos == null){
			return R.error("不能为空哦！");
		}
		//如果依旧是待审核，表明未修改，则直接返回
		if(projectinfos.getAuditstatus() == ConstantUtils.AUDIT){
			return R.ok();
		}
        //审核判错
        R auditError = projectinfosService.judgeAuditError(projectinfos,getUserId());
        if(auditError != null){
            return auditError;
        }
		projectinfosService.auditProject(projectinfos);
		return R.ok();
	}

    /**
     * 获取审核项目详细信息
     */
	@SysLog("查询审核项目信息")
    @GetMapping("/auditinfo/{projectinfoid}")
    @RequiresPermissions("projectinfo:projectinfos:auditinfo")
    public R auditinfo(@PathVariable("projectinfoid")String projectinfoid){
		if(projectinfoid == null){
			return R.error("不能为空哦！");
		}
        ProjectinfosCustomEntity projectinfos = projectinfosService.queryAuditInfo(projectinfoid);
        return R.ok().put("projectinfos", projectinfos);
    }

	
	/**
	 * 列表
	 */
	@SysLog("查询项目列表")
	@RequestMapping("/list")
	@RequiresPermissions("projectinfo:projectinfos:list")
	public R list(@RequestParam Map<String, Object> params){
		if(params == null){
			return R.error("不能为空哦！");
		}
		//查询列表数据
        Query query = new Query(params);
		List<ProjectinfosCustomEntity> projectinfosList = projectinfosService.queryCustomList(query);
		int total = projectinfosService.queryTotal(query);
		PageUtils pageUtil = new PageUtils(projectinfosList, total, query.getLimit(), query.getPage());
		return R.ok().put("page", pageUtil);
	}

	/**
	 * 简单列表（地区，项目名，创建者，项目类型）
	 */
	@SysLog("查询简单项目列表（地区，项目名，创建者，项目类型）")
	@RequestMapping("/desclist")
	@RequiresPermissions("projectinfo:projectinfos:list")
	public R queryDescList(@RequestParam Map<String, Object> params){
		if(params == null){
			return R.error("不能为空哦！");
		}
		if(params.containsKey("columnid")){
			String columnid = (String) params.get("columnid");
			//根据id查询地区号（地区号不是主键）
			String areaid = areainfosService.queryAreaidByColumnId(columnid);
			//判断地区为那一级
			int anInt = Integer.parseInt(areaid);
			//县/区
			int xian = anInt % 100;
			int shi = anInt % 10000;
			int shen = anInt % 1000000;
			if(xian > 0){
			//地区为县级

			}else if(shi > 0){
				//为市级
				shi += 1000;
				areaid += shi;
			}else if(shen > 0){
				//为省级
				shen += 10000;
				areaid += shen;
			}
		}
		//查询列表数据
		Query query = new Query(params);
		List<ProjectinfosCustomEntity> projectinfosList = projectinfosService.queryDescList(query);
		int total = projectinfosService.queryDescTotal(query);
		PageUtils pageUtil = new PageUtils(projectinfosList, total, query.getLimit(), query.getPage());
		return R.ok().put("page", pageUtil);
	}



	/**
	 * 信息
	 */
	@SysLog("查询项目信息")
	@GetMapping("/info/{projectinfoid}")
	@RequiresPermissions("projectinfo:projectinfos:info")
	public R info(@PathVariable("projectinfoid")String projectinfoid){
		if(projectinfoid == null){
			R.error("请选择项目！");
		}
		ProjectinfosCustomEntity projectinfos = projectinfosService.queryCustomInfo(projectinfoid);
		return R.ok().put("projectinfos", projectinfos);
	}
	
	/**
	 * 保存
	 */
//	@SysLog("保存项目信息")
	@PostMapping("/save")
	@RequiresPermissions("projectinfo:projectinfos:save")
	public R save(@RequestBody ProjectinfosEntity projectinfos){
		if(projectinfos == null){
			return R.error("请填写项目信息！");
		}
        //验证属性是否为空
        //添加pojo属性验证
        ValidatorUtils.validateEntity(projectinfos, AddGroup.class);

		//项目类型状态码
		R typeError = projectinfosService.judgeProjectTypeError(projectinfos.getProjecttype());
		if(typeError != null){
			return typeError;
		}
		//验证项目进度状态码
        R judgeProgressError = projectinfosService.judgeProgressError(projectinfos.getProjectprogress());
        if(judgeProgressError != null){
            return judgeProgressError;
        }
        //验证软删除状态码
        R isDelError = projectinfosService.judgeIsDelError(projectinfos.getIsdel());
        if(isDelError != null){
            return isDelError;
        }
		//验证地区
		R existArea = areainfosService.isExistArea(projectinfos.getAreainfoid());
		if(existArea != null){
			return existArea;
		}
		//验证用户
		R userError = userinfosService.judgeUserError(projectinfos.getCreateuser());
        if(userError != null){
        	return userError;
		}
		//验证审核
		R auditError = projectinfosService.judgeAuditError(projectinfos, getUserId());
		if(auditError != null){
			return auditError;
		}
		//生成项目的uuid，并设置给id
		UUID uuid = UUID.randomUUID();
		String id = uuid.toString().replaceAll("-", "");
		projectinfos.setProjectinfoid(id);
		//设置时间
		Date date = new Date();
		projectinfos.setCreatetime(date);
		projectinfos.setUpdatetime(date);
		projectinfosService.save(projectinfos);
		return R.ok();
	}
	
	/**
	 * 修改
	 */
	@SysLog("修改项目信息")
	@PostMapping("/update")
	@RequiresPermissions("projectinfo:projectinfos:update")
	public R update(@RequestBody ProjectinfosEntity projectinfos){
		if(projectinfos == null){
			return R.error("请先选择项目！");
		}
		//添加pojo属性验证
		ValidatorUtils.validateEntity(projectinfos, AddGroup.class);
		//项目类型状态码
		R typeError = projectinfosService.judgeProjectTypeError(projectinfos.getProjecttype());
		if(typeError != null){
			return typeError;
		}
		//验证项目进度状态码
		R judgeProgressError = projectinfosService.judgeProgressError(projectinfos.getProjectprogress());
		if(judgeProgressError != null){
			return judgeProgressError;
		}
		//验证软删除状态码
		R isDelError = projectinfosService.judgeIsDelError(projectinfos.getIsdel());
		if(isDelError != null){
			return isDelError;
		}
		//验证地区
		R existArea = areainfosService.isExistArea(projectinfos.getAreainfoid());
		if(existArea != null){
			return existArea;
		}
		//验证用户
		R userError = userinfosService.judgeUserError(projectinfos.getCreateuser());
		if(userError != null){
			return userError;
		}
		//验证审核
		R auditError = projectinfosService.judgeAuditError(projectinfos, getUserId());
		if(auditError != null){
			return auditError;
		}
		//设置更新时间
		projectinfos.setUpdatetime(new Date());
		//更新
		projectinfosService.update(projectinfos);
		return R.ok();
	}





	/**
	 * 删除
	 */
	@SysLog("删除项目信息")
	@PostMapping("/delete")
	@RequiresPermissions("projectinfo:projectinfos:delete")
	public R delete(@RequestBody String[] projectinfoids){
		if(projectinfoids == null){
			return R.error("请选择删除！");
		}
		projectinfosService.deleteBatch(projectinfoids);
		//删除项目所对应的所有收藏
		int i = collectinfosService.deleteByProject(projectinfoids);
		//删除项目所对应的足迹
		vipfootprintinfosService.deleteByProject(projectinfoids);
		return R.ok();
	}
	
}
